package com.atguigu.tingshu.payment.context;

import com.atguigu.tingshu.payment.strategy.WeChatPayStrategy;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class WeChatPayStrategyContext implements ApplicationContextAware {

    private static final Map<String , WeChatPayStrategy> STRATEGY_MAP = new HashMap<>() ;

    public static WeChatPayStrategy getWeChatPayStrategy(String paymentType) {
        return STRATEGY_MAP.get(paymentType) ;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        applicationContext.getBeansOfType(WeChatPayStrategy.class)
                .values().forEach(weChatPayStrategy -> {
                    STRATEGY_MAP.put(weChatPayStrategy.getPaymentType() , weChatPayStrategy) ;
                });
    }
}
